package com.beaglebros.SDWatch;

public class Bitmask {
	private int bitmap;
	private static int maxBit = Integer.SIZE;

	public Bitmask() {
		bitmap = 0;
	}
	
	public Bitmask(int bm) {
		bitmap = bm;
	}

	public int getBitmap() {
		return bitmap;
	}

	public void setBitmap(int bitmap) {
		this.bitmap = bitmap;
	}
	
	public int toInteger() {
		return bitmap;
	}
	
	public String toString() {
		return Integer.toString(bitmap);
	}
	
	public String toBinaryString() {
		return Integer.toBinaryString(bitmap);
	}
	
	public boolean getBit(int bit) throws BitmapOutOfBoundsException {
		if (bit >= maxBit) {
			throw new BitmapOutOfBoundsException();
		}
		return ((bitmap & (1<<bit)) != 0);
	}
	
	public int setBit(int bit) throws BitmapOutOfBoundsException {
		if (!this.getBit(bit)) {
			bitmap += (1<<bit);
		}	
		return bitmap;
	}
	
	public int clearBit(int bit) throws BitmapOutOfBoundsException {
		if (this.getBit(bit)) {
			bitmap -= (1<<bit);
		}
		return bitmap;
	}

	static public boolean getBit(int sbitmap, int bit) throws BitmapOutOfBoundsException{
		if (bit >= maxBit) {
			throw new BitmapOutOfBoundsException();
		}
		return ((sbitmap & (1<<bit)) != 0);
	}
	
	static public int setBit(int sbitmap, int bit) throws BitmapOutOfBoundsException {
		if (!getBit(sbitmap, bit)) {
			sbitmap += (1<<bit);
		}	
		return sbitmap;
	}
	
	static public int clearBit(int sbitmap, int bit) throws BitmapOutOfBoundsException {
		if (getBit(sbitmap, bit)) {
			sbitmap -= (1<<bit);
		}
		return sbitmap;
	}

}

class BitmapOutOfBoundsException extends Exception {
	private static final long serialVersionUID = -476197548700265168L;
	BitmapOutOfBoundsException() {
		super();
	}
}